<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar title="登录" class="page-nav-bar">
      <template #left>
        <van-icon name="cross" @click="$router.back()" />
      </template>
    </van-nav-bar>
    <!-- 登录表单 -->
    <!-- 给表单增加ref属性 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <!-- 正则表达式验证表单 -->
      <van-field
        name="mobile"
        placeholder="请输入手机号"
        v-model="user.mobile"
        :rules="userFormRules.mobile"
        clearable
        type="number"
        maxlength="11"
      >
        <!-- 增加图标 -->
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        name="code"
        placeholder="请输入验证码"
        v-model="user.code"
        :rules="userFormRules.code"
        type="number"
        maxlength="6"
      >
        <!-- 增加图标 -->
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <!-- 增加按钮 -->
        <!-- finish事件是事件结束时触发 -->
        <template #button>
          <van-count-down
            :time="6 * 1000"
            format="ss s"
            v-if="isCountDownShow"
            @finish="isCountDownShow = false"
          />
          <!--  native-type="button"是取消按键时表单提交 -->
          <van-button
            class="send-sms-btn"
            round
            size="small"
            native-type="button"
            type="default"
            @click="onSendSms"
            v-else
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div style="margin: 16px;" class="login-btn-wrap">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          class="login-btn"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user.js'
export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data() {
    return {
      user: {
        mobile: '',
        code: '',
      },
      // 正则表达式手机验证规则
      userFormRules: {
        mobile: [
          {
            required: true,
            message: '内容不能为空',
          },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号码格式错误' },
        ],
        code: [
          {
            required: true,
            message: '内容不能为空',
          },
          { pattern: /^\d{6}$/, message: '验证码格式错误' },
        ],
      },
      isCountDownShow: false,
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 1.点击登录事件
    async onSubmit() {
      //  1.获取表单数据
      const user = this.user
      // 2.表单验证
      this.$toast.loading({
        // 请求前,加载 禁用背景点击展示时长
        message: '加载中...',
        forbidClick: true,
        duration: 0,
      })
      // 3.提交请求登录
      try {
        const { data } = await login(user)
        console.log('登录成功', data)
        this.$toast.success('登录成功')
        // 登录成功之后，储存数据给vuex
        this.$store.commit('setUser', data.data)
        // 登录成功后返回原来页面
        this.$router.back()
      } catch (err) {
        if (err.response && err.response.status === 400) {
          this.$toast.fail('手机或者验证码错误')
        } else {
          // console.log('登录失败稍后重试', err)
          this.$toast.fail('登录失败稍后重试')
        }
      }
    },
    // 2.点击获取验证码事件
    async onSendSms() {
      try {
        // form表单验证
        await this.$refs.loginForm.validate('mobile')
        console.log('ok')
      } catch (err) {
        return console.log('手机号验证失败')
      }
      // 2.验证通过，显示倒计时
      this.isCountDownShow = true
      // 3.请求发送验证码
      try {
        await sendSms(this.user.mobile)
        this.$toast('发送成功')
      } catch (err) {
        this.isCountDownShow = false
        if (err.response && err.response.status === 429) {
          this.$toast('发送太频繁，请稍后再试')
        } else {
          this.$toast('发送失败，请稍后再试')
        }
      }
    },
  },
}
</script>

<style lang="less" scoped>
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
